<?php
/**
 * Created by PhpStorm.
 * User: huyi
 * Date: 2018/1/15
 * 消费者：接收消息
 * 逻辑：
 * 创建连接-->创建channel-->创建交换机-->创建队列-->绑定交换机/队列/路由键-->接收消息
 * 消费者
 */
//配置信息
$conn_args = array(
    'host'     => '127.0.0.1',
    'port'     => '5672',
    'login'    => 'huyi',
    'password' => 'huyi123',
    'vhost'    => '/'
);

$e_name  = 'e_huyi'; //交换机名
$q_name  = 'q_huyi'; //队列名
$k_route = 'key_1'; //路由key

//创建连接和channel
$conn = new AMQPConnection($conn_args);
if(!$conn->connect()){
    die("Cannot connect to the broker!\n");
}
$channel = new AMQPChannel($conn);

//创建交换机
$ex = new AMQPExchange($channel);
$ex->setName($e_name);
$ex->setType(AMQP_EX_TYPE_DIRECT); //direct类型
$ex->setFlags(AMQP_DURABLE); //持久化
echo "Exchange Status:" . $ex->declareExchange() . "\n";

//创建队列
$q = new AMQPQueue($channel);
$q->setName($q_name);
$q->setFlags(AMQP_DURABLE); //持久化
echo "Message Total:" . $q->declareQueue() . "\n";

//绑定交换机与队列，并指定路由键
echo 'Queue Bind: ' . $q->bind($e_name, $k_route) . "\n";

//阻塞模式接收消息
echo "Message:\n";
while(True){
    //$q->consume('processMessage');  //实现一进一出的应答
    $q->consume('processMessage', AMQP_AUTOACK); //自动ACK应答
}
$conn->disconnect();

/**
 * 消费回调函数
 * 处理消息
 */
function processMessage(AMQPEnvelope $envelope, AMQPQueue $queue){
    $msg = $envelope->getBody();
    echo $msg . "\n"; //处理消息
    $queue->ack($envelope->getDeliveryTag()); //手动发送ACK应答
}

